package com.example.hw_demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hw_demo.entity.User;
import com.example.hw_demo.service.UserService;
import com.example.hw_demo.util.Result;
import com.example.hw_demo.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    @GetMapping("/test")
    public Result test(){
        System.out.println(1/0);
        return new Result(200,"测试查询成功",userService.list());
        //return  userService.list();
    }
@GetMapping("/selectPage")
public Result selectPage(User user){
        return userService.selectPage(user);
}

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("user_account",user.getUserAccount())
                .eq("user_password",user.getUserPassword());
        User user1 = userService.getOne(queryWrapper);//单个对象查询
        //判断是否有值
        if(user1!=null){
            //登录成功
            String tokenString = new TokenUtil().createToken(user1);
            return  new Result(200,"登录成功",tokenString);
        }else{
            //登录失败
            return  new Result(500,"账号密码有误",null);
        }
    }
    /**
     * 注册接口
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        return new Result(200,"新增成功",userService.save(user));
    }

    //新增
    @PostMapping("/add")
    public Result add(@RequestBody User user){
        boolean bool = userService.save(user);
        return  new Result(200,"新增成功",bool);
    }
    //修改
    @PostMapping("/updata")
    public Result updata(@RequestBody User user){
        boolean bool = userService.updateById(user);
        return  new Result(200,"修改成功",bool);
    }

    //删除的接口
    @DeleteMapping("/deleteUser")
    public Result delete(int userId){
      boolean bool  = userService.removeById(userId);
        return  new Result(200,"删除成功",bool);
    }

    @PostMapping("/ailogin")
    public Result uploadImg(@RequestParam(value = "file") MultipartFile multipartFile){
        //1、判断上传文件是否为NULL;
        if(multipartFile.isEmpty()){
            return  new Result(500,"文件为null,请重新上传",null);
        }
        //2、把上传的文件名称改成不重复的
        String imgName = multipartFile.getOriginalFilename();//原始图片名称 xx.png
        //获取后缀名.png
        String  lowerString  = imgName.substring(imgName.lastIndexOf(".")).toLowerCase();
        //重新生成文件名称System.currentTimeMillis():获取系统时间的毫秒数  UUID生成名称
        String newName = System.currentTimeMillis()+lowerString;
        //3、保存文件、图片
        //判断文件夹是否存在，存在就往里放文件，不存在就先创建文件夹
        File file = new File("E:/aiLoginimg/");
        if(!file.exists()){
            //创建文件夹
            file.mkdir();
        }
        //把上传的文件放入文件夹
        //创建文件
        File file1 = new File("E:/aiLoginimg/",newName);
        try {
            multipartFile.transferTo(file1);//把上传的文件放入对应的磁盘中
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
         //照片跟用户头像比较
        return  userService.aiLogin("E:/aiLoginimg/",newName,"E://hw_img/");
    }

}
